package utility;



/**
 * Utility class with static utility methods.
 * 
 * @author siid
 */
public class Utility {

	
	/**
	 * Converts the given name (e.g. database fieldname or database tablename)
	 * into a java name.<br>
	 * <br>
	 * 
	 * Example:
	 * <li> prefix = get, name = REV_NUM - Return-Value: getRevNum
	 * <li> prefix = set, name = REV_NUM - Return-Value: setRevNum
	 * <li> firstUp = true, prefix = null, name = USER - Return-Value: User
	 * 
	 * @param firstUp
	 *            indicates whether the first character is upper chase or not,
	 *            e.g. true for classname
	 * @param prefix
	 *            a prefix, e.g. get, set
	 * @param name
	 *            the db fieldname
	 * 
	 * @return the converted name
	 */
	public static String createJavaName(boolean firstUp, String prefix,
			String name) {

		StringBuffer aResult = new StringBuffer();
		name = name.trim().toLowerCase();

		if (prefix != null) {
			aResult.append(prefix);
		}

		boolean upperCase = firstUp;
		for (int j = 0; j < name.length(); j++) {
			String aNextCharacter = name.substring(j, j + 1);
			if (aNextCharacter.equals("_")) {
				upperCase = true;
				continue;
			}
			if (upperCase) {
				aNextCharacter = aNextCharacter.toUpperCase();
			}
			aResult.append(aNextCharacter);
			upperCase = false;
		}
		
		String returnValue = aResult.toString();
		
		if (returnValue.equals("package")
				|| returnValue.equals("class")
				|| returnValue.equals("this")) {
			returnValue = "_" + returnValue;
		}

		return returnValue;
	}
	
	/**
	 * Converts the given name (e.g. database fieldname or database tablename)
	 * into a java name.<br>
	 * <br>
	 * 
	 * Example:
	 * <li> firstUp = true, prefix = null, name = USER - Return-Value: User
	 * 
	 * @param firstUp
	 *            indicates whether the first character is upper chase or not,
	 *            e.g. true for classname
	 * @param name
	 *            the db fieldname
	 * 
	 * @return the converted name
	 */
	public static String createJavaNameFromFieldName(boolean firstUp,
			String fieldname) {
		return createJavaName(firstUp, null, fieldname);
	}
}
